package com.itheima.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.springboot.common.Result;
import com.itheima.springboot.entity.Book;
import com.itheima.springboot.entity.User;
import com.itheima.springboot.mapper.BookMapper;
import com.itheima.springboot.mapper.UserMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName: UserController
 * @Description:
 * @Author: lanshou666
 * @Create: 2022-07-21 22:23
 */

@RestController
@RequestMapping("/book")
public class BookController {

    @Resource
    private BookMapper bookMapper;

    /*
     * @param user:
     * @return com.itheima.springboot.common.Result<?>
     * @author lanshou666
     *  保存图书
    */
    @PostMapping()
    public Result<?> save(@RequestBody Book book){
        bookMapper.insert(book);
        return Result.success();
    }

    @PutMapping()
    public Result<?> update(@RequestBody Book book){
        bookMapper.updateById(book);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id){
        bookMapper.deleteById(id);
        return Result.success();
    }

    @GetMapping
    public Result<?> find(@RequestParam(defaultValue = "1") Integer currentPage,
                          @RequestParam(defaultValue = "10") Integer pageSize,
                          @RequestParam(defaultValue = "") String search){
        LambdaQueryWrapper<Book> queryWrapper = Wrappers.<Book>lambdaQuery().like(Book::getName, search);
        if (StringUtils.isNotBlank(search)){
            queryWrapper.like(Book::getName,search);
        }
        Page<Book> bookPage = bookMapper.selectPage(new Page<>(currentPage, pageSize),queryWrapper);
        return Result.success(bookPage);
    }

    @PostMapping("/deleteBatch")
    public Result<?> deleteBatch(@RequestBody List<Integer> ids) {
        bookMapper.deleteBatchIds(ids);
        return Result.success();
    }

}
